package com.cunyme;

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.Toast;

public class esims_awards extends Activity {
	
	WebView mWebView;
	
	
	public void onCreate(Bundle emailweb) {
		super.onCreate(emailweb);
		

        SQLiteDatabase cuny_meDB = openOrCreateDatabase("cuny_me.db", Context.MODE_PRIVATE, null);
        helper_functions helpers = new helper_functions();
		Map <String, String> portal_cookies = helpers.getcookies("portal", cuny_meDB);
		Map <String, String> esims_cookies = helpers.getcookies("esims", cuny_meDB);
		cuny_meDB.close();
		
		try{
			
			//Connection esims_conn = Jsoup.connect("https://esims.cuny.edu/LoggedInServlet").data("CurrentCommand", "ChangeEmailEntry")
			Connection esims_conn = Jsoup.connect("https://esims.cuny.edu/LoggedInServlet?CurrentCommand=AwardsDisplay&Term=201102")
			.cookie("ObSSOCookie", portal_cookies.get("ObSSOCookie")).cookie("JSESSIONID", portal_cookies.get("JSESSIONID"))
            .cookie("BIGipServerportal_http_pl", portal_cookies.get("BIGipServerportal_http_pl"))
            .cookie("portalErrorCookie", portal_cookies.get("portalErrorCookie")).cookie("ObTEMP", portal_cookies.get("ObTEMP"))
            .cookie("JSESSIONID", esims_cookies.get("JSESSIONID"))
            .cookie("BIGipServeresims_new_http_pl", esims_cookies.get("BIGipServeresims_new_http_pl"));

			org.jsoup.nodes.Document doc = esims_conn.post();
            Element body = doc.body();
            String source = body.html();
            
    		setContentView(R.layout.esims_email);
    		Log.i("esims_email", source);

    		mWebView = (WebView) findViewById(R.id.webview);
    		mWebView.getSettings().setJavaScriptEnabled(true);
    		mWebView.loadDataWithBaseURL(null, source, "text/html", "UTF-8", null);

    		//mWebView.loadUrl("https://esims.cuny.edu/LoggedInServlet?CurrentCommand=TranscriptDisplay&DegreeTrack=1");
			
        }
        catch (IOException e)
        {
        	Toast.makeText(getApplicationContext(), "Unable to Establish Connection.", Toast.LENGTH_SHORT).show();
            // TODO Auto-generated catch block
            e.printStackTrace();
        }	
	}
}
